let express = require('express');
let router = express.Router();
let fs = require('fs');
let UUID = require('uuid');
let multer = require('multer');

let {uploadPath} = require('../utils/pathManager')

//upload.single  .array()
let upload = multer({dest: uploadPath});


let {usersPath} = require('../utils/pathManager');

/* GET home page. */
router.post('/register', function (req, res) {
  //原有的用户列表有没有该用户 有 : 400 坏的请求
  //username : 唯一标识
  //password
  //uuid : 唯一标识

  let userList = fs.readFileSync(usersPath, 'utf-8');
  if (!userList) {
    userList = [];
  } else {
    userList = JSON.parse(userList)
  }

  //处理请求过来的字段
  if (!req.body['username'] || !req.body['password']) {
    //返回一个400的错误

    res.status(422).send({
      code: 0,
      error: '注册失败, 必须要有字段 username|| password'
    });
    return
  }

  let index = userList.findIndex(item => item.username === req.body['username']);
  if (index !== -1) {
    res.status(400).send({
      code: 0,
      error: '注册失败, 用户名已存在'
    });
    return;
  }
  let uid = UUID.v1();
  console.log(uid);
  userList.push({
    uid,
    username: req.body['username'],
    password: req.body['password'],
    avatar: 'https://upload.wikimedia.org/wikipedia/commons/3/3d/Shaman-logo-128.png'
  });

  //存储到users.json
  fs.writeFileSync(usersPath, JSON.stringify(userList), 'utf-8');

  res.send({
    code: 1,
    msg: '注册成功'
  });
});
router.post('/login', function (req, res) {

  let userList = fs.readFileSync(usersPath, 'utf-8');
  if (!userList) {
    userList = [];
  } else {
    userList = JSON.parse(userList)
  }

  //处理请求过来的字段
  if (!req.body['username'] || !req.body['password']) {
    //返回一个400的错误

    res.status(422).send({
      code: 0,
      error: '登录失败, 必须要有字段 username|| password'
    });
    return
  }

  let index = userList.findIndex(item => item.username === req.body['username']);
  if (index !== -1) {
    if (userList[index].password === req.body['password']) {
      res.status(200).send({
        code: 1,
        data: {
          username: userList[index]['username'],
          uid: userList[index]['uid'],
          avatar: userList[index]['avatar'] || 'https://upload.wikimedia.org/wikipedia/commons/3/3d/Shaman-logo-128.png'
        },
        msg: '登录成功'
      });
    } else {
      res.status(400).send({
        code: 0,
        error: '密码出错'
      });
    }
  } else {
    res.status(400).send({
      code: 0,
      error: '此用户名不存在'
    });
  }
});
router.post('/changePwd', function (req, res) {
  res.send({
    code: 1,
    msg: '注册成功'
  });
});
router.get('/userInfo', function (req, res) {
  let uid = req.query.uid;
  let userList = fs.readFileSync(usersPath, 'utf-8');
  if (!userList) {
    userList = [];
  } else {
    userList = JSON.parse(userList)
  }
  let index = userList.findIndex(item => item.uid === uid);
  console.log(uid);
  res.send({
    code: 1,
    msg: '个人信息',
    data: userList[index]
  });
});
router.post('/userInfo', function (req, res) {
  let uid = req.body.uid;
  let avatar = req.body.avatar;
  //1. 读取users.json
  let userList = fs.readFileSync(usersPath, 'utf-8');
  if (!userList) {
    userList = [];
  } else {
    userList = JSON.parse(userList)
  }
  let index = userList.findIndex(item => item.uid === uid);
  if (index === -1) {
    //没有这个用户
    res.status(400).send({
      code: 1,
      msg: '没有此用户',
    });
    return
  } else {
    //修改信息
    if (avatar) {
      userList[index]['avatar'] = avatar;
      //存储到users.json
    }
    fs.writeFileSync(usersPath, JSON.stringify(userList), 'utf-8');
  }
  res.send({
    code: 1,
    msg: '修改成功',
    data: userList[index]
  });
});

/*
* 文件上传接口
* */
router.post('/upload', upload.single('avatar'), function (req, res) {
  res.send({
    code: 1,
    msg: '上传成功',
    data: {
      imgUrl: 'http://localhost:7007/' + req.file.filename
    }
  })
});


module.exports = router;
